﻿using Microsoft.AspNetCore.Http; 
using System.Text.RegularExpressions; 

namespace Agile.Infra.Core.Agile.Extensions
{
    public static class HttpContextExtensions
    {
        /// <summary>设置文件下载名称</summary>
        /// <param name="@this"></param>
        /// <param name="fileName"></param>
        public static HttpContext FileInlineHandle(this HttpContext @this, string fileName)
        {
            var encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
            @this.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
            return @this;
        }

        /// <summary>设置文件附件名称</summary>
        /// <param name="@this"></param>
        /// <param name="fileName"></param>
        public static HttpContext FileAttachmentHandle(this HttpContext @this, string fileName)
        {
            var encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
            @this.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
            return @this;
        }

        /// <summary>设置响应内容</summary>
        public static async Task WriteRespnseAsync(this HttpContext @this, string responseContent, string contentType = "text/html; charset=utf-8")
        {
            @this.Response.ContentType = contentType;
            await @this.Response.WriteAsync(responseContent);
        }

        /// <summary>获取语言种类</summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static string GetLanguage(this HttpContext @this)
        {
            string res = "zh-CN";
            var str = @this.Request.Headers["Accept-Language"].FirstOrDefault();
            if (str is not null)
            {
                res = str.Split(",")[0];
            }
            return res;
        }

        /// <summary>获取客户端IP</summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetClientIp(this HttpContext @this)
        {
            if (@this == null)
            {
                return "";
            }

            var result = @this.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(result))
            {
                result = @this.Connection.RemoteIpAddress?.ToString();
            }
            if (string.IsNullOrEmpty(result) || result.Contains("::1"))
            {
                result = "127.0.0.1";
            }

            result = result.Replace("::ffff:", "127.0.0.1");

            //Ip规则效验
            var regResult = Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

            return regResult ? result : "127.0.0.1";
        }

        /// <summary>获取浏览器标识</summary>
        /// <param name="@this"></param>
        /// <returns></returns>
        public static string GetUserAgent(this HttpContext @this) => @this.Request.Headers["User-Agent"];
         
    }
}
